package de.haw.shc.Szenario;

import android.app.ActionBar;
import android.app.Dialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import de.haw.shc.Config.TabsAdapter;
import de.haw.shc.Szenario.TimerSzenario.TimerAbotDialog;
import de.haw.shc.Szenario.WakeUpSzenario.WakeupAbotDialog;
import de.haw.shc.R;

/**
 * Created with IntelliJ IDEA.
 * User: mizus
 * Date: 29.06.13
 * Time: 16:26
 * To change this template use File | Settings | File Templates.
 */
public class SzenarioActivity extends FragmentActivity implements TimerAbotDialog, WakeupAbotDialog {
	private boolean timerCanceled = false;

    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.pagerview);


        buildTabs();
    }

    private void buildTabs() {

        ViewPager viewPager = new ViewPager(this);
        viewPager.setId(R.id.pager);

        setContentView(viewPager);

        ActionBar bar = getActionBar();
        // Create a new Action bar and set title to strings.xml

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setTitle(R.string.app_title);

        // Attach the Tabs to the fragment classes and set the tab title.
        TabsAdapter tabsAdapter = new TabsAdapter(this, viewPager);



        //iD Pager
        int iD = 0;
        viewPager.setOffscreenPageLimit(iD);

        Bundle bundle = new Bundle();

        //Love Szenario
        bundle.putString("Szenario", "Love");
        bundle.putInt("index", iD);
        viewPager.setOffscreenPageLimit(iD);
        tabsAdapter.addTab(bar.newTab().setText(""), LoveSzenario.class, bundle, R.drawable.icon_love ,getResources().getInteger(R.integer.love));

        // Einbrecher Szenario
 //       bundle.putString("Szenario", "Urlaub");
 //       bundle.putInt("index", iD++);
 //       viewPager.setOffscreenPageLimit(iD);
 //       tabsAdapter.addTab(bar.newTab().setText(""), housebreakerSzenario.class, bundle, R.drawable.einbrecher , getResources().getInteger(R.integer.eco));

        // Wecker Szenario
        bundle.putString("Szenario", "Wecker");
        bundle.putInt("index", iD++);
        viewPager.setOffscreenPageLimit(iD);
        tabsAdapter.addTab(bar.newTab().setText(""), WakeUpSzenario.class, bundle, R.drawable.icon_wakeup ,getResources().getInteger(R.integer.wakeup));

        // Timer Szenario
        bundle.putString("Szenario", "Timer");
        bundle.putInt("index", iD++);
        viewPager.setOffscreenPageLimit(iD);
        tabsAdapter.addTab(bar.newTab().setText(""), TimerSzenario.class, bundle, R.drawable.icon_timer ,getResources().getInteger(R.integer.timer));

    }

	@Override
	public void showTimerAbortDialog() {
//		Log.d("Dialog is showing: " ,  "=====Methode showTimerAbortDialog aufgerufen=====");
			final Dialog abort_Dialog = getAbortDialog();

            Button cancel_btn = (Button)abort_Dialog.findViewById(R.id.abortDialog_btn);
            cancel_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                	timerCanceled = true;
                    abort_Dialog.cancel();
                }
            });
        abort_Dialog.show();
	}

	@Override
	public boolean isNotificationCanceled() {
		return timerCanceled;
	}

	@Override
	public void showWakeupAbortDialog(final MediaPlayer mediaPlayer) {
		final Dialog abort_Dialog = getAbortDialog();

        Button cancel_btn = (Button)abort_Dialog.findViewById(R.id.abortDialog_btn);
        cancel_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	mediaPlayer.stop();
                abort_Dialog.cancel();
            }
        });
    abort_Dialog.show();
		
	}
	
	public Dialog getAbortDialog(){
		Dialog abort_Dialog = new Dialog(this) ;
        abort_Dialog.setContentView(R.layout.abort_dialog);
        abort_Dialog.setTitle(R.string.title_dialog);

        TextView lbl_Text = (TextView)abort_Dialog.findViewById(R.id.abortDialog_text);
        lbl_Text.setText(R.string.abortDialog_txt);
        return abort_Dialog;
	}
}
